# -*- coding: utf-8 -*-
'''
Created on 09/nov/2009

@author: sax
'''

import logging
LOGGER = logging.getLogger('xadmin.xamodeladmin')

from xadmin.options.strategies import Strategy
class PermissionStrategy(Strategy):
    
    def contribute_to_class(self, cls, name):
        print cls, name
        
    def __get__(self, owner, type=None):
        return self

    def __set__(self, owner, value):
        super(PermissionStrategy, self).__set__(owner, value)

        if value:
            class OptionEnhClass(owner.opts.__class__):
                def get_read_permission(self):
                    return 'read_%s' % self.object_name.lower()
    
                def get_read_field_restriction(self, field_name):
                    return 'cant_read_%s' % field_name
    
                def get_edit_field_restriction(self, field_name):
                    return 'cant_edit_%s' % field_name
             
            owner.opts.__class__ = OptionEnhClass  
    
            owner.has_read_permission = self.has_read_permission
            owner.has_add_permission = self.has_add_permission
            owner.has_read_field_restriction = self.has_read_field_restriction
            owner.has_change_permission = self.has_change_permission
            owner.has_delete_permission = self.has_delete_permission
            owner.get_model_perms = self.get_model_perms
        else:
            pass
#            owner.has_read_permission = \
#            owner.has_add_permission = \
#            owner.has_read_field_restriction = \
#            owner.has_change_permission = \
#            owner.has_delete_permission = lambda a,b : True

            
    
    def has_read_field_restriction(self, request=None, obj=None, field_name=None):
        if request.user.is_superuser:
            return False
        opts = self.opts
        perm_name = opts.get_read_field_restriction(field_name)
        perm_value = request.user.has_perm(opts.app_label + '.' + perm_name)

        LOGGER.debug("%s.%s : %s (%s) " % (obj, field_name, perm_name, perm_value))
        return perm_value
    
    def has_read_permission(self, request, obj=None): #IGNORE:W0613
        """
        Returns True if the given request has permission to change the given
        Django model instance.

        If `obj` is None, this should return True if the given request has
        permission to change *any* object of the given type.
        """
        LOGGER.debug("%s" % obj)
        opts = self.opts
        return request.user.has_perm(opts.app_label + '.' + opts.get_read_permission())
        
    def has_add_permission(self, request):
        "Returns True if the given request has permission to add an object."
        opts = self.opts
        return request.user.has_perm(opts.app_label + '.' + opts.get_add_permission())

    def has_change_permission(self, request, obj=None):
        """
        Returns True if the given request has permission to change the given
        Django model instance.

        If `obj` is None, this should return True if the given request has
        permission to change *any* object of the given type.
        """
        opts = self.opts
        return request.user.has_perm(opts.app_label + '.' + opts.get_change_permission())

    def has_delete_permission(self, request, obj=None):
        """
        Returns True if the given request has permission to change the given
        Django model instance.

        If `obj` is None, this should return True if the given request has
        permission to delete *any* object of the given type.
        """
        opts = self.opts
        return request.user.has_perm(opts.app_label + '.' + opts.get_delete_permission())

    def get_model_perms(self, request):
        """
        Returns a dict of all perms for this model. This dict has the keys
        ``add``, ``change``, and ``delete`` mapping to the True/False for each
        of those actions.
        """
        return {
            'add': self._owner.has_add_permission(request),
            'change': self._owner.has_change_permission(request),
            'delete': self._owner.has_delete_permission(request),
            'read': self._owner.has_read_permission(request),
        }    
        
        